package top.csmcool.musicapi.client;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import top.csmcool.musicapi.enums.PlatformEnum;
import top.csmcool.musicapi.model.api.kugou.*;

import top.csmcool.musicapi.util.MusicRequestUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Author shizhe
 * @Date 2025/2/8
 */

@Component
@RequiredArgsConstructor
public class KugouClient extends CommonClient {

    private final MusicRequestUtil musicRequestUtil;

    public List<MusicList> playlistDetail(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(),"playlistDetail");
        JSONObject entries = musicRequestUtil.sendRequestAutowiredJson(parameter, getUrl(parameter));
        if(entries == null || entries.isEmpty()) {
            return new ArrayList<>();
        }
        JSONArray data = entries.getJSONArray("data");
        return JSONUtil.toList(data, MusicList.class);
    }

    public List<Song> playlistTrackAll(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(),"playlistTrackAll");
        String url = getUrl(parameter) + generateGetRequestUrl(parameter);
        JSONObject entries = musicRequestUtil.sendRequestAutowiredJson(parameter, url);
        if(entries == null || entries.isEmpty()) {
            return new ArrayList<>();
        }
        JSONArray data = entries.getJSONObject("data").getJSONArray("songs");
        return JSONUtil.toList(data, Song.class);
    }

    public SearchLyric searchLyric(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(),"searchLyric");
        String url = getUrl(parameter) + generateGetRequestUrl(parameter);
        JSONObject entries = musicRequestUtil.sendRequestAutowiredJson(parameter, url);
        if(entries == null || entries.isEmpty()) {
            return new SearchLyric();
        }
        return JSONUtil.toBean(entries, SearchLyric.class);
    }

    public Lyric lyric(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(),"lyric");
        String url = getUrl(parameter) + generateGetRequestUrl(parameter);
        JSONObject entries = musicRequestUtil.sendRequestAutowiredJson(parameter, url);
        if(entries == null || entries.isEmpty()) {
            return new Lyric();
        }
        return JSONUtil.toBean(entries, Lyric.class);
    }

    public UserPlayList userPlaylist(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(),"userPlaylist");
        String url = getUrl(parameter) + generateGetRequestUrl(parameter);
        JSONObject entries = musicRequestUtil.sendRequestAutowiredJson(parameter, url);
        if(entries == null || entries.isEmpty()
                || entries.getJSONObject("data") == null
                || entries.getJSONObject("data").isEmpty()) {
            return new UserPlayList();
        }

        return JSONUtil.toBean(entries.getJSONObject("data"), UserPlayList.class);
    }

    public MusicCollectionResponse topPlaylist(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(),"topPlaylist");
        String url = getUrl(parameter) + generateGetRequestUrl(parameter);
        JSONObject entries = musicRequestUtil.sendRequestAutowiredJson(parameter, url);
        if(entries == null || entries.isEmpty()
                || entries.getJSONObject("data") == null
                || entries.getJSONObject("data").isEmpty()) {
            return new MusicCollectionResponse();
        }

        return JSONUtil.toBean(entries.getJSONObject("data"), MusicCollectionResponse.class);
    }

    public SongSearch search(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(),"search");
        String url = getUrl(parameter) + generateGetRequestUrl(parameter);
        JSONObject entries = musicRequestUtil.sendRequestAutowiredJson(parameter, url);
        if(entries == null
                || entries.isEmpty()
                || entries.getJSONObject("data") == null
                || entries.getJSONObject("data").isEmpty()) {
            return new SongSearch();
        }
        return JSONUtil.toBean(entries.getJSONObject("data"), SongSearch.class);
    }

    public MusicInfo songUrl(JSONObject parameter) {
        parameter.set(getMETHOD_NAME(),"songUrl");
        String url = getUrl(parameter) + generateGetRequestUrl(parameter);
        JSONObject entries = musicRequestUtil.sendRequestAutowiredJson(parameter, url);
        if(entries == null || entries.isEmpty() || entries.get("url") == null) {
            return null;
        }
        return JSONUtil.toBean(entries, MusicInfo.class);
    }


    @Override
    public PlatformEnum getPlatform() {
        return PlatformEnum.KUGOU;
    }
}
